package action;

import dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Servlet implementation class Deng
 */
@WebServlet("/Deng")
public class Deng extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Deng() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("zml");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String flag = request.getParameter("flag");
		if (flag == null || flag.equals("query")) {
			String name = request.getParameter("username");
			String pwd = request.getParameter("password");
			System.out.println(name);
			System.out.println(pwd);
			boolean isok = UserDao.deng(name, pwd);
			String me = "";
			if (isok) {
				me = "成功";
				//response.getWriter().print(true);
				request.getSession().setAttribute("name", name);
				request.getSession().setAttribute("pwd", pwd);
				request.getRequestDispatcher("index.jsp").forward(request,response);
			} else {
				me = "失败";
				request.getRequestDispatcher("login.jsp").forward(request,response);
			//	response.getWriter().print(false);
			}
			System.out.println(me);
		}else if(flag.equals("zhuce")){
			System.out.println("z12323ml");
			//Integer id=Integer.parseInt(request.getParameter("id"));
			String name = request.getParameter("username");
			String pwd = request.getParameter("userpwd");
			int isok = UserDao.xiu(name, pwd);
			
			if (isok>0) {
				System.out.println("注册成功");
				response.getWriter().print(true);
			} else {
				System.out.println("注册失败");
				response.getWriter().print(false);
				
			}
		}
	}
}
